package com.fjw.car_send;

import android.graphics.RectF;

public class TouchPoint {
    private float width = 0;
    private float height = 0;

    private float startX = 0;
    private float startY = 0;

    private long startTime = 0;
    private long inervalTime = 800;

    private float currentX = 0;
    private float currentY = 0;

    private int id = 0;

    private boolean touched = false;

    private RectF positiveRegion;
    private RectF negativeRegion;

    public float getWidth() {
        return width;
    }

    public float getHeight() {
        return height;
    }

    public float getStartX() {
        return startX;
    }

    public float getStartY() {
        return startY;
    }

    public float getCurrentX() {
        return currentX;
    }

    public float getCurrentY() {
        return currentY;
    }

    public int getId() {
        return id;
    }

    public boolean isTouched() {
        return touched;
    }

    public void setTouched(int id, float x, float y) {
        this.touched = true;
        this.id = id;
        this.startX = x;
        this.startY = y;
        this.startTime = System.currentTimeMillis();
        this.currentX = x;
        this.currentY = y;
    }

    public void release() {
        this.touched = false;
        this.startX = 0;
        this.startY = 0;
        this.currentX = 0;
        this.currentY = 0;
        this.id = 0;
    }

    public void move(float x, float y) {
        this.currentX = x;
        this.currentY = y;
    }

    public int getLevelX() {
        int level = (int) ((this.currentX - this.startX) / ((this.width - this.width / 3) * 0.5) * 3);
        long current = System.currentTimeMillis();
        if (level < 0) {
            return -3;
        } else if (level > 0) {
            return 3;
        } else if (current - startTime > inervalTime) {
            if (positiveRegion != null && positiveRegion.contains(startX, startY)) {
                return 3;
            } else if (negativeRegion != null && negativeRegion.contains(startX, startY)) {
                return -3;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

    public int getLevelY() {
        int level = (int) -((this.currentY - this.startY) / (this.height * 0.5) * 3);
        if (level < -3) {
            return -3;
        } else if (level > 3) {
            return 3;
        } else if (level == 0) {
            if (System.currentTimeMillis() - startTime > inervalTime) {
                if (positiveRegion != null && positiveRegion.contains(startX, startY)) {
                    return 1;
                } else if (negativeRegion != null && negativeRegion.contains(startX, startY)) {
                    return -1;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
        } else {
            return level;
        }
    }

    public void setScreen(float width, float height, RectF positiveRegion, RectF negativeRegion) {
        this.width = width;
        this.height = height;
        this.positiveRegion = positiveRegion;
        this.negativeRegion = negativeRegion;
    }
}
